﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_My_Commitment_Default : LoggedInPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        listCommitmentDates.ItemDataBound += new EventHandler<ListViewItemEventArgs>(listCommitmentDates_ItemDataBound);
        listCommitmentDates.DataBound += new EventHandler(listCommitmentDates_DataBound);
        dsDates.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsDates_Select);
        
        if (!IsPostBack)
        {
            var selectedDate = DateTime.MinValue;
            if (Request.QueryString["Date"] != null)
            {
                try
                {
                    selectedDate = DateTime.Parse(Request.QueryString["Date"]);
                }
                catch (FormatException) { }
            }
            if (selectedDate != DateTime.MinValue && CommitmentDates.Contains(selectedDate))
            {
                listCommitmentDates.SelectedIndex = Array.IndexOf(CommitmentDates, selectedDate);
            }
            else
            {
                listCommitmentDates.SelectedIndex = 0;
            }
            DataBind();
        }
    }

    void listCommitmentDates_DataBound(object sender, EventArgs e)
    {
        if (listCommitmentDates.Items.Count == 0)
        {
            noData.Visible = true;
            lnkActionPlan.NavigateUrl = AcmsContext.ResolveUrl("~/MyCO2/MyActionPlan.aspx", this);
        }
    }

    void listCommitmentDates_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.EmptyItem)
        {
            var lnkActionPlan = e.Item.FindControl("lnkActionPlan") as HyperLink;
            lnkActionPlan.NavigateUrl = AcmsContext.ResolveUrl("~/MyCO2/MyActionPlan.aspx", this);
        }
    }

    private DateTime[] _CommitmentDates;
    protected DateTime[] CommitmentDates
    {
        get
        {
            if (_CommitmentDates == null)
            {
                CarbonExchange.Bll.CommitmentFactory factory = new CarbonExchange.Bll.CommitmentFactory();
                _CommitmentDates = factory.GetCommitmentDatesByUserId(User.Identity.User.Id);
            }
            return _CommitmentDates;
        }
    }

    void dsDates_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(CommitmentDates);
    }

    protected void listSections_DataBinding(object sender, EventArgs e)
    {
        var listSections = sender as ListView;
        listSections.DataSource = new CarbonExchange.Bll.SectionFactory().GetAll();
    }

    protected void listCommitments_DataBinding(object sender, EventArgs e)
    {
        ListView listCommitments = sender as ListView;
        ListViewDataItem listSectionsItem = listCommitments.NamingContainer as ListViewDataItem;
        ListView listSections = listSectionsItem.NamingContainer as ListView;
        //rptSections.data
        int sectionId = (int)listSections.DataKeys[listSectionsItem.DisplayIndex]["Id"];
        listCommitments.DataSource = Commitements.Where(x => x.SectionId == sectionId).ToArray();
        listCommitments.ItemDataBound += new EventHandler<ListViewItemEventArgs>(listCommitments_ItemDataBound);
    }

    protected void listCommitments_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            CarbonExchange.Bll.Commitment commitment = ((ListViewDataItem)e.Item).DataItem as CarbonExchange.Bll.Commitment;
            CheckBox cboCommitment = e.Item.FindControl("cboCommitment") as CheckBox;
            cboCommitment.Text = new CarbonExchange.Bll.PledgeFactory().GetById(commitment.PledgeId).Text;
            cboCommitment.Checked = commitment.Completed;
            cboCommitment.Enabled = !cboCommitment.Checked;
            
        }
    }

    private List<CarbonExchange.Bll.Commitment> commitments;
    protected List<CarbonExchange.Bll.Commitment> Commitements
    {
        get
        {
            if (commitments == null)
            {
                var date = (DateTime)listCommitmentDates.SelectedDataKey["Date"];
                commitments = new CarbonExchange.Bll.CommitmentFactory().GetAllByUserIdAndCommitmentDate(User.Identity.User.Id, date).ToList();
            }
            return commitments;
        }
    }

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        if (IsValid)
        {
            var listSections = listCommitmentDates.Items[listCommitmentDates.SelectedIndex].FindControl("listSections") as ListView;
            foreach (var sectionItem in listSections.Items)
            {
                var listCommitments = sectionItem.FindControl("listCommitments") as ListView;
                foreach (var commitmentItem in listCommitments.Items)
                {
                    CheckBox cboCommitment = commitmentItem.FindControl("cboCommitment") as CheckBox;
                    var id = (int)listCommitments.DataKeys[commitmentItem.DisplayIndex]["Id"];
                    Commitements.Where(x => x.Id == id).Single().Completed = cboCommitment.Checked;

                    cboCommitment.Enabled = !cboCommitment.Checked;
                }
            }
            new CarbonExchange.Bll.CommitmentFactory().Save(Commitements);
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AcmsContext.RedirectToUrl("~/MyCO2/MyActionPlan.aspx", this);
    }

}